<extend name="Base/common" />

<block name="style">
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/Api/css/order.css">
</block>

<block name="Content">

<div class="page-group">
    <div class="page page-current">
        <div class="content my-order-coupon">
            <!--优惠券1-->
            <div class="exchange-top">
                <input type="text" name="code" id="code" placeholder="请输入优惠码">
                <a href="#" class="right" onclick="exchange()">兑换</a>
            </div>
            <div class="buttons-tab">
                <a href="#tab_1" onclick="changeTab(this);" class="tab-link button active">可使用</a>
                <a href="#tab_2" onclick="changeTab(this);" class="tab-link button">已使用</a>
                <input type="hidden" name="lastIndex" id="lastIndex" value="0"/>
                <input type="hidden" name="tabIndex" id="tabIndex" value="{$tabIndex}"/>
            </div>
            <div class="tabs">
                <div id="tab_1" class="tab active infinite-scroll">
                    <div class="list-block media-list fff">
                        <ul>
                        	
                        </ul>
                    </div>
                    <div class="infinite-scroll-preloader">
						<div class="preloader">
                      	</div>
                    </div>
                </div>
                
                <div id="tab_2" class="tab infinite-scroll">
                    <div class="list-block media-list fff">
                        <ul>
                        
                        
                        </ul>
                    </div>
                    <div class="infinite-scroll-preloader">
						<div class="preloader">
                      	</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</block>

<block name="script">
<script type="text/javascript">

var target = Think.U('UserCoupon/ajax_couponList');
var params = ['lastIndex', 'tabIndex'];
var container = '.infinite-scroll.active .fff ul';

$("#lastIndex").val('0');

var tb = $("#tabIndex").val();
tb = tb ? tb : '1';
$("a[class*='tab-link']").removeClass('active');
$("a[class*='tab-link']").eq(tb-1).addClass('active');

function changeTab(obj){
	
	var tabHref = $(obj).attr('href');
	var index = tabHref.indexOf('_');
	var tabIndex = tabHref.substr(index+1);

	if(!isNaN(tabIndex)){
		
		$("#tabIndex").val(tabIndex);
		
		if($("#tab_" + tabIndex).find('.infinite-scroll-preloader').css("display")==='block'){
			
			var itemLength = $("#tab_" + tabIndex).find('li').length;
			
			$("#lastIndex").val(itemLength);
			$.init();
			
			if(itemLength == 0){
				addItems();
			}
		}
	}
}

function joinHTML(data){
	
	var html = '';

	if($.isArray(data) && data.length > 0){
		
		var idStr = $('.infinite-scroll.active').attr('id');
		var tabIndex = '1';
		
		if(idStr){
			var index = idStr.indexOf('_');
			tabIndex = idStr.substr(index+1);
		}
		
		$.each(data, function(key, val){
			
			var color = '';
			var type = '';
			var code = '';
			var category = '<span class="activity">活动</span>';
			
			if(tabIndex == 2){
				color = ' coupon-dis';
			}
			
			if(val['minimum'] > 0){
				type = '<li><i class="list-disc"></i>满' + val['minimum'] + '元可用</li>';
			}
			
			if(val['category'] == 1){
				category = '<span class="first-use">首次</span>';
			}else if (val['category'] == 2){
				code = '<li><i class="list-disc"></i>优惠码' + $.trim(val['code']) + '</li>';
				category = '<span class="invite">邀请</span>';
			}
			
			html += '<li><div class="coupon' + color + '">';
			html += '<div class="coupon-text"><p>帮工小哥</p><div class="coupon-detail clearfix">';
			html += '<span class="left coupon-money">￥' + val['money'] + '</span><div class="coupon-use right">';
			html += '<p class="coupon-date">' + formatDate(val['create_time']) + '--' + formatDate(val['deadline']);
			html += '</p></div></div></div><div class="condition"><ul>' + type + code + '</ul></div>';
			html += '<div class="mark">' + category + '</div></div></li>';
			
		});
	}else{
		
		html = '<div class="content-block place-order"><p>暂无可用优惠券</p></div>';
	}

	return html;
}

function exchange(){
	
	var code = $("#code").val();
	if(!(/^\d{8}$/.test(code))){
		$.toast('优惠码格式不正确，请确认');
		return false;
	}
	
	var target = Think.U('UserCoupon/exchange');
	
	$.post(target, {'code':code}, function(data){
		
		if(data.status == '-201'){
			$.alert('身份认证已失效，请重新登录', function () {
		        window.location.href = Think.U('Public/login');
		    });
		}else if(data.status == '422'){
			$.alert('参数异常');
		}else if(data.status == '0'){
			$.alert('优惠码不存在');
		}else if(data.status == '2'){
			$.alert('优惠券已被使用，不可兑换');
		}else if(data.status == '3'){
			$.alert('优惠券已过期，不可兑换');
		}else if(data.status == '-1'){
			$.alert('优惠券所有者为本人，不可兑换');
		}else if(data.status == '1'){
			$.alert('兑换成功', function(){
				window.location.reload();
			});
		}
		
	});
}

</script>
<script src="__PUBLIC__/Api/js/pageLoading.js" type="text/javascript"></script>
</block>

